<?php
namespace app\api\controller;
use app\api\logic\Task;
use think\Db;
use app\company\logic\Message;
use app\common\lib\SendIntegralNew;
use think\Log;

class Crontab
{
    //获取公司列表
    public function get_company(){
        $list = M('company')->where('status',1)->field('id')->select();
        return $list;
    }
    //公告定时任务 日任务
    public function notice(){
        $time = time();
        $list = $this->get_company();
        foreach ($list as $key=>$value){
            $where = [];
            $where['com_id'] = $value['id'];
            $where['status'] = 2;//公示中
            $where['end_time'] = ['lt',$time];//过期
            $notice = M('com_notice')->where($where)->select();
            foreach ($notice as $k1=>$v1){
                $where_1 = [];
                $where_1['notice_id'] = $v1['id'];
                $where_1['status'] = 0;
                $notice_user = M('com_notice_relation')->where($where_1)->group('user_id')->select();
                //未读扣分
                foreach ($notice_user as $k2=>$v2){
                    $v1['user_id'] = $v2['user_id'];
                    SendIntegralNew::add_record_integral($v1,-abs($v1['not_read_integral']),0,22,true);
                }
            }
            //修改状态
            M('com_notice')->where($where)->update(['status'=>3]);
        }
    }
    //任务定时任务 日任务
    public function task(){
        $time = time();
        $list = $this->get_company();
        foreach ($list as $key=>$value){
            //报名截止
            $where = [];
            $where['com_id'] = $value['id'];
            $where['apply_end_time'] = ['lt',$time];
            $where['status'] = 2; //报名中
            M('com_task')->where($where)->update(['status'=>3]);
            //任务截止
            $where = [];
            $where['com_id'] = $value['id'];
            $where['end_time'] = ['lt',$time];
            $where['status'] = 3; //进行中
            $task = M('com_task')->where($where)->select();
            //超期负责人扣分
            foreach ($task as $k1=>$v1){
                if($v1['del_type'] == 1){ //等值扣分
                    $integral = $v1['del_integral'];
                }elseif($v1['del_type'] == 2){//翻倍扣分
                    $day = ceil(($time - $v1['end_time'])/(24*3600));
                    $integral = $v1['del_integral_double']*pow(2,$day-1);
                    if($integral > $v1['double_max']){
                        $integral = $v1['double_max'];
                    }
                }
                $v1['user_id'] = $v1['principal_user_id'];
                SendIntegralNew::add_record_integral($v1,-abs($integral),0,18,true);
            }
        }
    }
    //工龄分结算 日任务
//    public function working_age_day(){
//        $time = time();
//        $list = $this->get_company();
//        foreach ($list as $key=>$value){
//            //工龄分配置
//            $sys = M('com_config')->where('com_id',$value['id'])->find();
//            if(empty($sys)){
//                break;
//            }
//            if(empty($sys['working_age_integral'])||empty($sys['working_age_end'])){
//                break;
//            }
//            //日结算
//            if($sys['working_age_type'] == 1 && $sys['working_age_end'] > $time){
//                $where['com_id'] =$value['id'];
//                $userUpdate = [
//                    'working_age_integral' => Db::raw('working_age_integral+' . $sys['working_age_integral']??0),
//                ];
//                M('users')->where($where)->update($userUpdate);
//            }
//        }
//    }
    //奖扣任务消息 日任务
    public function prize_buckle_task(){
        $time = time();
        $list = $this->get_company();
        foreach ($list as $key=>$value){
            //查询奖扣任务的用户
            $users = M('com_prize_buckle_config_relation_users')->where('com_id',$value['id'])->select();
            foreach ($users as $k1=>$v1){
                //发送消息
                $data = Task::get_prize_buckle_task_state($v1['user_id']);
                $data['com_id'] = $value['id'];
                $data['user_id'] = $v1['user_id'];
                $data['cate_type'] = 6;
                $data['title'] = '奖扣任务每日小结';
                $data['name'] = date('Ymd',$time);
                $data['create_time'] = $time;
                Message::send_prize_buckle_task_msg($data);
            }
        }
    }
    //定时任务  删除未支付的订单  日任务
    public function del_order(){
        $ids = M('goods_order')->where('status',0)->column('id');
        //删除订单产品
        M('goods_order_relation')->where('goods_order_id','in',$ids)->delete();
        //删除订单
        M('goods_order')->where('status',0)->delete();
    }
    //定时任务  检测企业到期时间  日任务
    public function detection_company(){
        $where= [];
        $where['status'] = 1;
        $where['use_time'] = ['lt',time()];
        $ids = M('company')->where($where)->column('id');
        if(!empty($ids)){
            M('company')->where('id','in',$ids)->update(['status'=>0]);
        }
    }
    //定时任务  未确认收货的订单7天后自动收货  日任务
    public function order_complete(){
        $where= [];
        $where['status'] = 2;
        $time = time() - 7*24*3600;
        $where['send_time'] = ['lt',$time];
        $list = M('goods_order')->where($where)->select();
        foreach ($list as $key=>$value){
            //修改状态
            M('goods_order')->where('id',$value['id'])->update(['status'=>3]);
            //加销量减库存
            $list_goods = M('goods_order_relation')->where('goods_order_id',$value['id'])->select();
            foreach ($list_goods as $k=>$v){
                $data = [
                    'sales'=>Db::raw('sales+'.$v['goods_num']),
                    'inventory'=>Db::raw('inventory-'.$v['goods_num']),
                ];
                M('goods')->where('id',$v['goods_id'])->update($data);
            }

        }

    }
    //工龄积分 月任务
//    public function working_age_month(){
//        $time = time();
//        $list = $this->get_company();
//        foreach ($list as $key=>$value){
//            //工龄分配置
//            $sys = M('com_config')->where('com_id',$value['id'])->find();
//            if(empty($sys)){
//                break;
//            }
//            if(empty($sys['working_age_integral'])||empty($sys['working_age_end'])){
//                break;
//            }
//            //月结算
//            if($sys['working_age_type'] == 0 && $sys['working_age_end'] > $time){
//                $where['com_id'] =$value['id'];
//                $userUpdate = [
//                    'integral_b' => Db::raw('integral_b+' . $sys['working_age_integral']??0),
//                ];
//                M('users')->where($where)->update($userUpdate);
//            }
//        }
//    }
    //考勤统计 月任务
    public function work_state(){
        $time = time();
        //上个月时间
        $start_time_month = strtotime('-1 month',strtotime(date('Y-m-01',$time)));
        $end_time_month = strtotime('+1 month',$start_time_month) -1;
        $list = $this->get_company();
        foreach ($list as $key=>$value){
            $where = [];
            $where['a.com_id'] = $value['id'];
            $where['a.start_time'] = ['between',[$start_time_month,$end_time_month]];
            $where['a.status'] = 2;
            $where['b.status'] = 1;
            $users = M('com_leave a')
                ->join('users b','b.id=a.user_id','left')
                ->where($where)
                ->group('a.user_id')
                ->field('sum(a.day) day,sum(a.hour) hour,a.user_id')
                ->select();
            //全勤奖分
            $integral = M('com_config')->where('com_id',$value['id'])->value('all_integral');
            if(empty($integral)){
                continue;
            }
            //有请假的用户
            foreach ($users as $k1=>$v1){
                //获得全勤
                if($v1['day'] == 0 && $v1['hour'] < 4){
                    $v1['com_id'] = $value['id'];
                    $v1['name'] = '全勤奖分';
                    $v1['type'] = 4;
                    SendIntegralNew::add_record_integral($v1,abs($integral),0,27,true,$end_time_month,3);
                }
            }
            //没请假的用户
            $user_ids = array_column($users,'user_id');
            $where_a = [];
            $where_a['com_id'] = $value['id'];
            $where_a['status'] = 1;
            $where_a['id'] = ['not in',$user_ids];
            $users_a = M('users')->where($where_a)->field('id')->select();
            foreach ($users_a as $k2=>$v2){
                //获得全勤
                $v3 = [];
                $v3['com_id'] = $value['id'];
                $v3['name'] = '全勤奖分';
                $v3['type'] = 4;
                $v3['user_id'] = $v2['id'];
                SendIntegralNew::add_record_integral($v3,abs($integral),0,27,true,$end_time_month,3);
            }
        }
    }
    //固定积分 月任务  每月1号发放
    public function fixed_integral(){
        $list = $this->get_company();
        foreach ($list as $key=>$value){
            $where = [];
            $where['a.com_id'] = $value['id'];
            $users = M('com_fixed_relation_users a')
                ->join('com_fixed_integral b','b.id=a.fixed_id','left')
                ->where($where)
                ->field('a.user_id,b.integral,a.fixed_id,b.name,a.com_id')
                ->select();
            foreach ($users as $k1=>$v1){
                //发送固定积分
                if($v1['integral'] != 0) {
                    SendIntegralNew::add_record_integral($v1, abs($v1['integral']), 0, 32, true, 0, 1);
                }
            }
        }
    }
    //奖扣任务统计 月任务 本月1号统计上月数据
    public function prize_buckle_task_state(){
        $time = time();
        $time = date('Y-m',strtotime('-1 month',$time));
        $time_1 = strtotime($time.'-01');
        $list = $this->get_company();
        foreach ($list as $key=>$value) {
            $where = [];
            $where['com_id'] = $value['id'];
            $users = M('com_prize_buckle_config_relation_users')->where($where)->select();
            foreach ($users as $k1=>$v1){
                //统计数据
                $data = Task::get_prize_buckle_task($v1['user_id'],$time);
                $data_array = [
                    'com_id'=>$value['id'],
                    'user_id'=>$v1['user_id'],
                    'prize_integral_target'=>$data['prize_integral_target'],//奖分目标
                    'buckle_integral_target'=>$data['buckle_integral_target'],//扣分目标
                    'prize_integral_type'=>$data['prize_buckle_type'],//奖扣分类型 1月任务  2周任务
                    'rate_target'=>$data['rate_target'],//比例目标
                    'people_target'=>$data['people_target'],//人次目标
                    'people_type'=>$data['people_type'],//人次类型   1日任务  2周任务  3月任务
                    'total_prize_integral'=>$data['prize_integral_com'],//总奖分
                    'total_buckle_integral'=>$data['buckle_integral_com'],//总扣分
                    'total_people'=>$data['people_com'],//总人次
                    'total_rate'=>$data['rate_com'],//总比例
                    'prize_integral_del'=>$data['prize_integral_del'],//奖分未完成总扣分
                    'buckle_integral_del'=>$data['buckle_integral_del'],//扣分未完成总扣分
                    'people_del'=>$data['people_del_total'],//人次未完成总扣分
                    'rate_del'=>$data['rate_del'],//比例未完成总扣分
                    'year'=>date('Y',$time_1),//年
                    'month'=>date('m',$time_1),//月
                    'year_month'=>strtotime(date('Y-m',$time_1)),//年月时间
                    'create_time'=>$time_1,//添加时间
                    'prize_status'=>$data['prize_integral_status'],//奖分完成状态  0未完成  1已完成
                    'buckle_status'=>$data['buckle_integral_status'],//扣分完成状态 0未完成 1已完成
                    'rate_status'=>$data['rate_status'],//比例完成状态  0未完成  1已完成
                    'people_status'=>$data['people_status'],//人次完成状态  0未完成  1已完成
                    'people_target_del'=>$data['people_del'],//人次未完成单次扣分
                    'total_del_integral'=>$data['total_del_integral'],//总扣分
                    'prize_buckle_task_name'=>$data['prize_buckle_task_name'],//奖扣任务名称
                ];
                //添加奖扣记录
                M('com_prize_buckle_task_log')->insert($data_array);
                //添加积分记录
//                $data_log[] = [
//                    'com_id'=>$value['id'],
//                    'user_id'=>$v1['user_id'],
//                    'type'=>2,
//                    'integral_b'=>-abs($data['prize_integral_del']),//奖分未完成扣分
//                    'create_time'=>$time_1,
//                    'year'=>date('Y',$time_1),//年
//                    'month'=>date('m',$time_1),//月
//                    'year_month'=>strtotime(date('Y-m',$time_1)),//年月时间
//                    'prize_buckle_type'=>1
//                ];
//                $data_log[] = [
//                    'com_id'=>$value['id'],
//                    'user_id'=>$v1['user_id'],
//                    'type'=>2,
//                    'integral_b'=>-abs($data['buckle_integral_del']),//扣分未完成扣分
//                    'create_time'=>$time_1,
//                    'year'=>date('Y',$time_1),//年
//                    'month'=>date('m',$time_1),//月
//                    'year_month'=>strtotime(date('Y-m',$time_1)),//年月时间
//                    'prize_buckle_type'=>2
//                ];
//                $data_log[] = [
//                    'com_id'=>$value['id'],
//                    'user_id'=>$v1['user_id'],
//                    'type'=>2,
//                    'integral_b'=>-abs($data['people_del_total']),//人次未完成扣分
//                    'create_time'=>$time_1,
//                    'year'=>date('Y',$time_1),//年
//                    'month'=>date('m',$time_1),//月
//                    'year_month'=>strtotime(date('Y-m',$time_1)),//年月时间
//                    'prize_buckle_type'=>3
//                ];
//                $data_log[] = [
//                    'com_id'=>$value['id'],
//                    'user_id'=>$v1['user_id'],
//                    'type'=>2,
//                    'integral_b'=>-abs($data['rate_del']),//比例未完成扣分
//                    'create_time'=>$time_1,
//                    'year'=>date('Y',$time_1),//年
//                    'month'=>date('m',$time_1),//月
//                    'year_month'=>strtotime(date('Y-m',$time_1)),//年月时间
//                    'prize_buckle_type'=>4
//                ];
//                M('com_integral_log')->insertAll($data_log);
                //发放奖分
                //奖分
                if(!empty($data['prize_integral_del'])) {
                    $info = [
                        'com_id' => $value['id'],
                        'user_id' => $v1['user_id'],
                        'type' => 1,
                        'name' => '奖扣任务奖分扣分',
                    ];
                    SendIntegralNew::add_record_integral($info, -abs($data['prize_integral_del']), 0, 35, true, $time_1, $type = 2);
                }
                //扣分
                if(!empty($data['buckle_integral_del'])) {
                    $info = [
                        'com_id' => $value['id'],
                        'user_id' => $v1['user_id'],
                        'type' => 2,
                        'name' => '奖扣任务扣分扣分',
                    ];
                    SendIntegralNew::add_record_integral($info, -abs($data['buckle_integral_del']), 0, 36, true, $time_1, $type = 2);
                }
                //比例
                if(!empty($data['rate_del'])) {
                    $info = [
                        'com_id' => $value['id'],
                        'user_id' => $v1['user_id'],
                        'type' => 4,
                        'name' => '奖扣任务比例扣分',
                    ];
                    SendIntegralNew::add_record_integral($info, -abs($data['rate_del']), 0, 37, true, $time_1, $type = 2);
                }
                //人次
                if(!empty($data['people_del_total'])) {
                    $info = [
                        'com_id' => $value['id'],
                        'user_id' => $v1['user_id'],
                        'type' => 3,
                        'name' => '奖扣任务人次扣分',
                    ];
                    SendIntegralNew::add_record_integral($info, -abs($data['people_del_total']), 0, 38, true, $time_1, $type = 2);
                }
            }
            //替换下个月的奖扣配置和人员
            //查询配置
            $config = M('com_prize_buckle_config a')
                ->join('com_prize_buckle_config_next b','b.config_id = a.id','left')
                ->where('a.com_id',$value['id'])
                ->field('a.id a_id,b.*')
                ->select();
            foreach ($config as $k2=>$v2){
                //替换奖扣配置
                $data_config = [];
                $data_config_next = [];
                if($v2['is_rate'] == 1){
                    $data_config['rate'] = $v2['rate'];
                    $data_config['is_rate'] = 1;
                    $data_config_next['rate'] = 0;
                    $data_config_next['is_rate'] = 0;
                }
                if($v2['is_man'] == 1){
                    $data_config['is_man'] = 1;
                    $data_config['man_type'] = $v2['man_type'];
                    $data_config['man_num'] = $v2['man_num'];
                    $data_config['man_deduct'] = $v2['man_deduct'];
                    $data_config_next['is_man'] = 0;
                    $data_config_next['man_type'] = 1;
                    $data_config_next['man_num'] = 0;
                    $data_config_next['man_deduct'] = 0;
                }
                if($v2['is_integral'] == 1) {
                    $data_config['is_integral'] = 1;
                    $data_config['integral_type'] = $v2['integral_type'];
                    $data_config['integral_reward'] = $v2['integral_reward'];
                    $data_config['integral_deduct'] = $v2['integral_deduct'];
                    $data_config_next['is_integral'] = 0;
                    $data_config_next['integral_type'] = 0;
                    $data_config_next['integral_reward'] = 0;
                    $data_config_next['integral_deduct'] = 0;
                }
                M('com_prize_buckle_config')->where('id',$v2['a_id'])->update($data_config);
                M('com_prize_buckle_config_next')->where('id',$v2['id'])->update($data_config_next);
                //替换人员配置
                $users = M('com_prize_buckle_config_relation_users_next')->where('prize_buckle_id',$v2['id'])->select();
                foreach ($users as $k3=>$v3){
                    if($v3['type'] == 1){
                        $data_users = [
                            'com_id'=>$value['id'],
                            'prize_buckle_id'=>$v2['a_id'],
                            'user_id'=>$v3['user_id'],
                        ];
                        //添加用户
                        M('com_prize_buckle_config_relation_users')->insert($data_users);
                    }elseif ($v3['type'] == 2){
                        //删除用户
                        M('com_prize_buckle_config_relation_users')->where(['user_id'=>$v3['user_id'],'prize_buckle_id'=>$v2['a_id']])->delete();
                    }
                }
                //删除下个月表用户
                M('com_prize_buckle_config_relation_users_next')->where('prize_buckle_id',$v2['id'])->delete();
            }
        }
    }
    //清除前一个月的数据 月任务 本月1号执行
    public function del_msg_data(){
        $where= [];
        $time = time() - 30*3600*24;
        $where['create_time'] = ['elt',$time];
        M('com_user_message')->where($where)->delete();
    }
    //统计用户积分
    public function update_user_integral_b(){
        $users = M('users')->field('id,total_integral_b')->select();
        $time = strtotime('2022-01-01')-1;
//        $data = [];
        foreach ($users as $key=>$value){
//            Log::info($key);
//            $data[] = Log::write($key);
//            Log::write($key);
//            $integral_b = SendIntegralNew::statistic_integral(strtotime('2021-01-01'),$time,$value['id'],0);
//            M('users')->where('id',$value['id'])->update(['total_integral_b'=>$value['total_integral_b']+$integral_b]);
        }
//        var_dump($data);
    }
    //数据分割
    public function update_integral_data(){
        $where = [];
        $time = strtotime('2021-01-01');
        $where['prize_buckle_time'] = ['lt',$time];
        $prize_buckle_ids = M('com_integral_prize_buckle')->where($where)->column('id');
        $where1 = [];
        $where1['prize_buckle_id'] = ['in',$prize_buckle_ids];
        $event_ids = M('com_integral_prize_buckle_relation_event')->where($where1)->column('id');
        $where2 = [];
        $where2['event_id'] = ['in',$event_ids];
        M('com_integral_event_relation_users')->where($where2)->delete();
        M('com_integral_prize_buckle_relation_event')->where($where1)->delete();
        M('com_integral_prize_buckle')->where($where)->delete();

    }








}
